package cn.wolfcode.web.controller;


import cn.wolfcode.domain.MessageBoard;
import cn.wolfcode.mapper.MessageReplyMapper;
import cn.wolfcode.qo.MyAJAX;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IMessageBoardService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/messageBoard")
public class MessageBoardController {

    @Autowired
    private IMessageBoardService messageBoardService;
    @Autowired
    private MessageReplyMapper replyMapper;


    @RequestMapping("/留言板分页页面")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<MessageBoard> pageInfo = messageBoardService.query(qo);
        model.addAttribute("PageInfo", pageInfo);
        return "留言板分页页面";
    }





    @RequestMapping("/save")
    @ResponseBody
    public MyAJAX saveOrUpdate(MessageBoard messageBoard) {
        if (messageBoard.getId() == null) {
            messageBoardService.save(messageBoard);
        }
        return new MyAJAX();
    }


    @RequestMapping("/留言详情查看页面")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("messageBoard",messageBoardService.get(id));
            model.addAttribute("messageReply",replyMapper.selectByBoard(id));
        }
        return "留言详情查看页面";
    }
}
